package com.qdlc.p2p.manage.action.additional;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.AdditionalDrawLotteryRuleService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.exception.DrawLotteryException;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.AdditionalDrawLotteryRule;
import com.qdlc.p2p.dal.dto.Operator;
import com.qdlc.p2p.dal.model.AdditionalDrawLotteryRuleModel;

/**
 * Created by liaoxiangkai on 2015/11/26.
 */
public class ManageDrawLotteryRuleAction extends BaseAction<AdditionalDrawLotteryRuleModel> implements ModelDriven<AdditionalDrawLotteryRuleModel> {
    @Resource
    private AdditionalDrawLotteryRuleService additionalDrawLotteryRuleService;

    private AdditionalDrawLotteryRuleModel model = new AdditionalDrawLotteryRuleModel();

    public AdditionalDrawLotteryRuleModel getModel() {
        return model;
    }

    private Map<String, Object> data;

    /**
     * ---------------抽奖规则---------------------
     * 进入页面
     *
     * @return
     * @throws Exception
     */
    @Action("/modules/additional/drawLottery/drawLotteryManager")
    public String drawLotteryManager() throws Exception {
        return "drawLotteryManager";
    }

    /**
     * 列表
     *
     * @throws Exception
     */
    @Action("/modules/additional/drawLottery/drawLotteryList")
    public void drawLotteryList() throws Exception {
        data = new HashMap<String, Object>();
        PageDataList<AdditionalDrawLotteryRuleModel> pageDataList = additionalDrawLotteryRuleService.list(model);
        data.put("total", pageDataList.getPage().getTotal());
        data.put("rows", pageDataList.getList());
        printJson(getStringOfJpaObj(data));
    }

    /**
     * --------------------添加抽奖规则--------------------------
     * 进入页面
     *
     * @return
     * @throws Exception
     */
    @Action("/modules/additional/drawLottery/drawLotteryAddPage")
    public String drawLotteryAddPage() throws Exception {
        return "drawLotteryAddPage";
    }

    /**
     * 抽奖规则添加动作
     *
     * @throws Exception
     */
    @SuppressWarnings("static-access")
    @Action("/modules/additional/drawLottery/drawLotteryAdd")
    public void drawLotteryAdd() throws Exception {
        Operator operator = getOperator();
        if (null == operator) {
            printResult("请先登录！", false);
            return;
        }
        model.setCreator(operator);
        model.setCreateTime(DateUtil.getNow());
        //检测添加抽奖规则参数
        checkDrawLotteryRule(model);
        //保存抽奖方案
        AdditionalDrawLotteryRule additionalDrawLotteryRule = additionalDrawLotteryRuleService.save(model);
        if (additionalDrawLotteryRule == null) {
            printResult("添加抽奖规则失败！", false);
            return;
        }
        printResult("添加抽奖规则成功！", true);
    }

    /**
     * -------------------编辑抽奖规则-----------------------------
     * 进入页面
     *
     * @return
     * @throws Exception
     */
    @Action("/modules/additional/drawLottery/drawLotteryEditPage")
    public String drawLotteryEditPage() throws Exception {
        AdditionalDrawLotteryRuleModel lotteryRule = additionalDrawLotteryRuleService.find(model.getId());
        if (lotteryRule == null) {
            printResult("该抽奖规则不存在!", false);
            return "drawLotteryManager";
        }
        request.setAttribute("lotteryRule", lotteryRule);
        return "drawLotteryEditPage";
    }

    /**
     * 抽奖规则更新动作
     *
     * @throws Exception
     */
    @Action("/modules/additional/drawLottery/doDrawLotteryEdit")
    public void doDrawLotteryEdit() throws Exception {
        Operator operator = getOperator();
        if (null == operator) {
            printResult("请先登录！", false);
            return;
        }
        //检测添加抽奖规则参数
        checkDrawLotteryRule(model);
        //设置更新者更新时间
        model.setModifier(operator);
        model.setModifyTime(DateUtil.getNow());
        //更新抽奖规则
        AdditionalDrawLotteryRule rule = additionalDrawLotteryRuleService.update(model);
        if (null == rule) {
            printResult("修改抽奖规则失败！", true);
            return;
        }
        printResult("修改抽奖规则成功！", true);
    }

    /**
     * ----------------抽奖规则详情页面-------------------
     *
     * @return
     * @throws Exception
     */
    @Action("/modules/additional/drawLottery/drawLotteryDetailPage")
    public String drawLotteryDetailPage() throws Exception {
        AdditionalDrawLotteryRuleModel lotteryRule = additionalDrawLotteryRuleService.find(model.getId());
        if (lotteryRule == null) {
            printResult("该抽奖规则不存在!", false);
            return "drawLotteryManager";
        }
        request.setAttribute("lotteryRule", lotteryRule);
        return "drawLotteryDetailPage";
    }

    /**
     * --------------------是否开启本条规则-----------------------
     * 开启规则
     *
     * @throws IOException
     */
    @Action("/modules/additional/drawLottery/drawLotteryEnable")
    public void drawLotteryEnable() throws IOException {
        Operator operator = getOperator();
        if (operator != null) {
            model.setModifier(operator);
            model.setModifyTime(DateUtil.getNow());
        }
        int result = additionalDrawLotteryRuleService.toggleStatus(model);
        if (result > 0) {
            printWebSuccess();
        } else {
            printWebResult("启用失败，请确认活动方案是否已开启！", false);
        }
    }

    /**
     * 关闭规则
     *
     * @throws IOException
     */
    @Action("/modules/additional/drawLottery/drawLotteryDisable")
    public void drawLotteryDisable() throws IOException {
        Operator operator = getOperator();
        if (operator != null) {
            model.setModifier(operator);
            model.setModifyTime(DateUtil.getNow());
        }
        int result = additionalDrawLotteryRuleService.toggleStatus(model);
        if (result > 0) {
            printWebSuccess();
        } else {
            printWebResult("关闭失败，请确认活动规则是否已关闭！", false);
        }
    }


    /**
     * ------------------校验方法-----------------------
     * 检验添加抽奖规则参数
     *
     * @param model
     * @return
     */
    private void checkDrawLotteryRule(AdditionalDrawLotteryRule model) throws Exception {
        if (null == model) {
            throw new DrawLotteryException("设置抽奖规则模型为空!");
        }
        if (model.getContinueCount() <= 0) {
            throw new DrawLotteryException("设置抽奖规则连续动作次数不合法!");
        }
        if (model.getValidDay() <= 0) {
            throw new DrawLotteryException("设置抽奖规则有效天数不合法!");
        }
        if (model.getStatus() != 0 && model.getStatus() != 1) {
            throw new DrawLotteryException("设置抽奖规则状态不合法!");
        }
        if (model.getCount() <= 0) {
            model.setCount(1);
        }
    }
}
